﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;

using System.Text;
using System.Windows.Forms;
using Fodee.BusinessComponent;
using Fodee.DataEntity;
using Fodee.Common;

namespace FodeeAdmin.SystemSettings
{
    public partial class StaffForm : BaseSystemSettingsForm
    {
        public StaffForm()
        {
            InitializeComponent();
            this.grid.AutoGenerateColumns = false;

            BindDropdownlist();
        }

        private fodStaffBC bc = new fodStaffBC();
        private fodDepartmentBC departmentbc = new fodDepartmentBC();

        public void InitForm()
        {
        }

        public void BindData()
        {
            FodeeDS = bc.GetAll();
            this.grid.DataSource = FodeeDS.fod_Staff.DefaultView;
            this.grid.ClearSelection();

            
            ClearForm();
        }

        public void BindDropdownlist()
        {
            FodeeDataSet dsDepartment = departmentbc.GetAll();
            this.cbxStaffDepartment.DataSource = dsDepartment.fod_Department.DefaultView;
            this.cbxStaffDepartment.DisplayMember = "DepartmentName";
            this.cbxStaffDepartment.ValueMember = "ID";
        }

        public override void ClearForm()
        {
            base.ClearForm();
            this.grid.ClearSelection();
            SetOperation(Operation.Add);
        }

        public void DeleteData()
        {
            bool result = bc.Delete(KeyID);
            if (result)
            {
                MessageBox.Show("删除成功");
                BindData();
            }
            else
            {
                MessageBox.Show("删除失败");
            }
        }

        public void SetOperation(Operation operation)
        {
            if (operation == Operation.Add)
            {
                this.btnSave.Text = "添加";
            }
            else
            {
                this.btnSave.Text = "修改";
            }

            FormOperation = operation;
        }

        public void AddData()
        {
            FodeeDataSet ds = new FodeeDataSet();
            FodeeDataSet.fod_StaffRow row = ds.fod_Staff.Newfod_StaffRow();
            row.StaffNo = this.tbxStaffNo.Text;
            row.StaffName = this.tbxStaffName.Text;
            row.StaffDepartmentID = int.Parse(this.cbxStaffDepartment.SelectedValue.ToString());
            row.StaffDepartmentName = this.cbxStaffDepartment.Text;
            row.StaffGender = this.rdbtnMale.Checked ? true : false;
            row.StaffIdentityCard = this.tbxStaffIdentityCard.Text;
            row.StaffMobileNo = this.tbxStaffMobileNo.Text;
            row.StaffContactNo = this.tbxStaffContactNo.Text;
            row.StaffRemarks = this.tbxStaffRemarks.Text;
            Util.SetCommonFields(row);
            ds.fod_Staff.Addfod_StaffRow(row);
            bool result = bc.Insert(ds);
            if (result)
            {
                MessageBox.Show("添加成功");
                BindData();
            }
            else
            {
                MessageBox.Show("添加失败");
            }
        }

        public void UpdateData()
        {
            FodeeDataSet.fod_StaffRow row = FodeeDS.fod_Staff.FindByID(KeyID);
            row.StaffNo = this.tbxStaffNo.Text;
            row.StaffName = this.tbxStaffName.Text;
            row.StaffDepartmentID = int.Parse(this.cbxStaffDepartment.SelectedValue.ToString());
            row.StaffDepartmentName = this.cbxStaffDepartment.Text;
            row.StaffGender = this.rdbtnMale.Checked ? true : false;
            row.StaffIdentityCard = this.tbxStaffIdentityCard.Text;
            row.StaffMobileNo = this.tbxStaffMobileNo.Text;
            row.StaffContactNo = this.tbxStaffContactNo.Text;
            row.StaffRemarks = this.tbxStaffRemarks.Text;
            bool result = bc.Update(FodeeDS);
            if (result)
            {
                MessageBox.Show("修改成功");
                BindData();
            }
            else
            {
                MessageBox.Show("修改失败");
            }
        }

        public void FillData()
        {
            FodeeDataSet.fod_StaffRow row = FodeeDS.fod_Staff.FindByID(KeyID);
            this.tbxStaffNo.Text = row.StaffNo;
            this.tbxStaffName.Text = row.StaffName;
            this.cbxStaffDepartment.SelectedValue = row.StaffDepartmentID;
            if (!row.IsStaffGenderNull() && row.StaffGender)
            {
                this.rdbtnMale.Checked = true;
            }
            else
            {
                this.rdbtnFemale.Checked = true;
            }
            this.tbxStaffIdentityCard.Text = row.IsStaffIdentityCardNull() ? "" : row.StaffIdentityCard;
            this.tbxStaffMobileNo.Text = row.IsStaffMobileNoNull() ? "" : row.StaffMobileNo;
            this.tbxStaffContactNo.Text = row.IsStaffContactNoNull() ? "" : row.StaffContactNo;
            this.tbxStaffRemarks.Text = row.IsStaffRemarksNull() ? "" : row.StaffRemarks;

            SetOperation(Operation.Edit);

            if (!base.ValidateForm())
            {
                return;
            }
        }


        private void StaffEditForm_Load(object sender, EventArgs e)
        {
            BindData();
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            if (!base.ValidateForm())
            {
                return;
            }

            if (FormOperation == Operation.Edit)
            {
                UpdateData();
            }
            else
            {
                AddData();
            }
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            ClearForm();
        }

        private void StaffForm_Load(object sender, EventArgs e)
        {
            BindData();
        }

        private void grid_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.RowIndex < 0 || e.ColumnIndex < 0)
            {
                return;
            }

            KeyID = int.Parse(this.grid.Rows[e.RowIndex].Cells["ID"].Value.ToString());
            string action = this.grid.Columns[e.ColumnIndex].Name;
            if (action.ToLower() == "delete")
            {
                bool isDelete = MessageBox.Show("确定删除吗?", "删除提示", MessageBoxButtons.OKCancel) == DialogResult.OK;
                if (isDelete)
                {
                    DeleteData();
                }
            }
            else
            {
                FillData();
            }
        }
    }
}
